package com.apestech.framework.util;

import com.apestech.api.tables.SequenceManage;
import com.apestech.api.tables.modul.SeqField;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;

/**
 * @author hup
 * @create 2019-02-19-18:19
 */
public class DBPub {

    public static long getIdKey(SqlSession session, String tabName) throws Exception {
        SequenceManage sequenceManage=new SequenceManage(session);
        try {
            return sequenceManage.getNextNum(tabName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getIdKeyStr(SqlSession session, String tabName) throws Exception {
        SequenceManage sequenceManage=new SequenceManage(session);
        try {
            return sequenceManage.getNextNumByStr(tabName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void resetSeq(SqlSession session, String tabName) throws Exception {
        SequenceManage sequenceManage=new SequenceManage(session);
        try {
            sequenceManage.reset(tabName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static  void resetSql_Create(SqlSession session, String tabName) throws Exception {
        SequenceManage sequenceManage=new SequenceManage(session);
        try {
            sequenceManage.reset(tabName);
        } catch (Exception e) {
            SeqField seqField=new SeqField();
            if(seqField==null){
                seqField=new SeqField();
                seqField.setPadding(12);
            }
            seqField.setTableId(tabName);
            seqField.setTableName(tabName);
            try{
                sequenceManage.createSeq(seqField, true);
                sequenceManage.reset(tabName);
            } catch (Exception e1) {
                throw new RuntimeException(e1);
            }
        }
    }

    public static void dropSequence(SqlSession session, String tabName) throws Exception {
        SequenceManage sequenceManage=new SequenceManage(session);
        try {
            session.getForUpdate().update("DELETE EXE_ID WHERE TABLE_ID='"+tabName+"'");
            String k=sequenceManage.getSeqName(0, tabName);
            sequenceManage.dropSequence(k);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void setSeqFromTable(SqlSession session, String tabName) throws Exception {
        SequenceManage sequenceManage=new SequenceManage(session);
        try {
            Object num=getTableId(session, tabName);
            if(num!=null){
                JSONArray list=session.getForJSON().query("SELECT * FROM EXE_ID WHERE TABLE_ID='"+tabName+"'");
                JSONObject m=list.get(0);
                if(m!=null){
                    SeqField seqField=new SeqField();
                    seqField.setTableId(tabName);
                    seqField.setTableName(m.getString("TABLE_NAME"));
                    seqField.setPadding(m.getInteger("PADDING"));
                    seqField.setRange(m.getLongValue("RANGE"));
                    seqField.setPrefix(m.getString("PREFIX"));
                    seqField.setSuffix(m.getString("SUFFIX"));
                    seqField.setId(m.getInteger("ID"));
                    seqField.setType(m.getInteger("TYPE"));
                    seqField.setNum(m.getLongValue("NUM"));
                    seqField.setCycle(m.getInteger("CYCLE"));
                    long n=sequenceManage.getNumByFieldValue(seqField, num);
                    if(n>0) sequenceManage.setNum(tabName, n+1);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static Object getTableId(SqlSession session, String tabName) throws Exception {
        Table tab=session.getDictTable(tabName);
        String pk=tab.getPkKey().getPkColumn();
        pk=pk.replaceAll(",","||");
        String sql="SELECT MAX("+pk+") AS NUM FROM "+tabName;
        JSONArray qlist=session.getForJSON().query(sql);
        if(qlist.size()==0) return null;
        return qlist.get(0).get("NUM");
    }

}
